package com.ruoyi.business.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.business.domain.JtOutboundOrderItem;
import com.ruoyi.business.domain.JtWarehouse;
import com.ruoyi.business.service.IJtWarehouseService;
import com.ruoyi.business.utils.SnGenerateUtil;
import com.ruoyi.common.annotation.RepeatSubmit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.JtOutboundOrder;
import com.ruoyi.business.service.IJtOutboundOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出库单Controller
 * 
 * @author Alan Zhang
 * @date 2025-06-19
 */
@Controller
@RequestMapping("/business/outbound")
public class JtOutboundOrderController extends BaseController
{
    private String prefix = "business/outbound";

    @Autowired
    private IJtOutboundOrderService jtOutboundOrderService;
    @Autowired
    private IJtWarehouseService jtWarehouseService;

    @RequiresPermissions("business:outbound:view")
    @GetMapping()
    public String outbound()
    {
        return prefix + "/outbound";
    }

    /**
     * 查询出库单列表
     */
    @RequiresPermissions("business:outbound:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JtOutboundOrder jtOutboundOrder)
    {
        startPage();
        List<JtOutboundOrder> list = jtOutboundOrderService.selectJtOutboundOrderList(jtOutboundOrder);
        return getDataTable(list);
    }

    /**
     * 导出出库单列表
     */
    @RequiresPermissions("business:outbound:export")
    @Log(title = "出库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JtOutboundOrder jtOutboundOrder)
    {
        List<JtOutboundOrder> list = jtOutboundOrderService.selectJtOutboundOrderList(jtOutboundOrder);
        ExcelUtil<JtOutboundOrder> util = new ExcelUtil<JtOutboundOrder>(JtOutboundOrder.class);
        return util.exportExcel(list, "出库单数据");
    }

    /**
     * 查看 出库单
     */
    @RequiresPermissions("business:outbound:view")
    @GetMapping("/view/{outboundOrderId}")
    public String view(@PathVariable("outboundOrderId") Long outboundOrderId, ModelMap mmap)
    {
        JtOutboundOrder jtOutboundOrder = jtOutboundOrderService.selectJtOutboundOrderByOutboundOrderId(outboundOrderId);
        JtWarehouse jtWarehouse = jtWarehouseService.selectJtWarehouseByWarehouseId(jtOutboundOrder.getWarehouseId());
        jtOutboundOrder.setWarehouseName(jtWarehouse.getWarehouseName());
        mmap.put("jtOutboundOrder", jtOutboundOrder);
        return prefix + "/view";
    }


    /**
     * 新增出库单
     */
    @RequiresPermissions("business:outbound:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("outboundOrderNo", SnGenerateUtil.outboundOrderNumber());
        return prefix + "/add";
    }

    /**
     * 新增保存出库单
     */
    @RequiresPermissions("business:outbound:add")
    @Log(title = "出库单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(JtOutboundOrder jtOutboundOrder)
    {
        Long totalQty = 0L;
        BigDecimal totalAmount = BigDecimal.ZERO;
        for(JtOutboundOrderItem item: jtOutboundOrder.getJtOutboundOrderItemList()){
             BigDecimal amount = item.getUnitPrice().multiply(BigDecimal.valueOf(item.getQty()));
            item.setAmount(amount);
            totalQty += item.getQty();
            totalAmount = totalAmount.add(amount);
        }
        jtOutboundOrder.setTotalAmount(totalAmount);
        jtOutboundOrder.setTotalQty(totalQty);
        return toAjax(jtOutboundOrderService.insertJtOutboundOrder(jtOutboundOrder));
    }

    /**
     * 修改出库单
     */
    @RequiresPermissions("business:outbound:edit")
    @GetMapping("/edit/{outboundOrderId}")
    public String edit(@PathVariable("outboundOrderId") Long outboundOrderId, ModelMap mmap)
    {
        JtOutboundOrder jtOutboundOrder = jtOutboundOrderService.selectJtOutboundOrderByOutboundOrderId(outboundOrderId);

        JtWarehouse jtWarehouse = jtWarehouseService.selectJtWarehouseByWarehouseId(jtOutboundOrder.getWarehouseId());
        jtOutboundOrder.setWarehouseName(jtWarehouse.getWarehouseName());
        mmap.put("jtOutboundOrder", jtOutboundOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存出库单
     */
    @RequiresPermissions("business:outbound:edit")
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JtOutboundOrder jtOutboundOrder)
    {
        Long totalQty = 0L;
        BigDecimal totalAmount = BigDecimal.ZERO;
        for(JtOutboundOrderItem item: jtOutboundOrder.getJtOutboundOrderItemList()){
            BigDecimal amount = item.getUnitPrice().multiply(BigDecimal.valueOf(item.getQty()));
            item.setAmount(amount);
            totalQty += item.getQty();
            totalAmount = totalAmount.add(amount);
        }
        jtOutboundOrder.setTotalAmount(totalAmount);
        jtOutboundOrder.setTotalQty(totalQty);
        return toAjax(jtOutboundOrderService.updateJtOutboundOrder(jtOutboundOrder));
    }

    /**
     * 删除出库单
     */
    @RequiresPermissions("business:outbound:remove")
    @Log(title = "出库单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jtOutboundOrderService.deleteJtOutboundOrderByOutboundOrderIds(ids));
    }


    /**
     * 确认 出库单
     */
    @RequiresPermissions("business:outbound:remove")
    @Log(title = "出库单-确认", businessType = BusinessType.UPDATE)
    @PostMapping( "/outbound")
    @ResponseBody
    public AjaxResult outbound(String ids)
    {
        return jtOutboundOrderService.outboundByIds(ids);
    }
}
